package com.shwread.android.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.shwread.android.qysw.activity.ReadingBookActivity;
import com.shwread.android.qysw10000243.R;

import org.geometerplus.android.fbreader.SetSwitchBackGrounAction;
import org.geometerplus.fbreader.fbreader.ActionCode;

import java.io.IOException;


public class PopWindowBg
        extends
        ReadingPopWindow
        implements
        OnItemClickListener
{
	private GridView gridView;
	private String[] imageRes;
	private String[] txtRes;
	private String   path = "wallpapers/";
	
	public static ReadingPopWindow getInstance(Context context) {
		return new PopWindowBg(context);
	}
	
	private PopWindowBg(Context context) {
		View contentView = LayoutInflater.from(context).inflate(R.layout.menu_pop_bg, null);
		init(context, contentView);
	}
	@Override
    protected void initView(View parentView) {
		gridView = (GridView) parentView.findViewById(R.id.menu_pop_gl);
		BgAdapter adapter = new BgAdapter();
		gridView.setAdapter(adapter);
		imageRes = new String[] {
		        path + "reading__shared__theme_shading_pattern0.png",
		        path + "reading__shared__theme_shading_pattern1.png",
		        path + "reading__shared__theme_shading_pattern7.png",
		        path + "reading__shared__theme_shading_pattern5.png",
		};
		txtRes = context.getResources().getStringArray(R.array.menu_bg_type_array);
		
    }
	
	@Override
	protected void addListener() {
		gridView.setOnItemClickListener(this);
	}
	
	private class BgAdapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			return 4;
		}
		
		@Override
		public Object getItem(int position) {
			return null;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null)
			{
				convertView = LayoutInflater.from(context).inflate(R.layout.menu_bg_type_item, null);
				holder = new ViewHolder();
				holder.imageView = (ImageView) convertView.findViewById(R.id.menu_bg_item_iv);
				holder.textView = (TextView) convertView.findViewById(R.id.menu_bg_item_tv);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.textView.setText(txtRes[position]);
			Bitmap bm;
			try
			{
				bm = BitmapFactory.decodeStream(context.getAssets().open(imageRes[position]));
				holder.imageView.setImageBitmap(bm);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return convertView;
		}
		
		class ViewHolder
		{
			TextView  textView;
			ImageView imageView; 
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		{
			((ReadingBookActivity) context).myFBReaderApp.runAction(ActionCode.SWITCH_TO_DAY_PROFILE);
			((ReadingBookActivity) context).myFBReaderApp.runAction(ActionCode.SWITCH_TO_CHANGE_BRIGHT, new SetSwitchBackGrounAction((ReadingBookActivity)context,((ReadingBookActivity) context).myFBReaderApp));
			((ReadingBookActivity) context).setBackGroud(imageRes[position]);
		}
	}

	
}
